package com.rong.shop.service.impl;

import com.rong.shop.mapper.OrderItemMapper;
import com.rong.shop.mapper.OrderMapper;
import com.rong.shop.pojo.Address;
import com.rong.shop.pojo.Order;
import com.rong.shop.pojo.OrderItem;
import com.rong.shop.pojo.vo.CartVO;
import com.rong.shop.service.IAddressService;
import com.rong.shop.service.ICartService;
import com.rong.shop.service.IOrderService;
import com.rong.shop.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    ICartService cartService;

    @Autowired
    IAddressService addressService;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    /**
     *  通过前端传来的 aid, uid, 和选中的cids；来生成订单-订单子项
     * @param aid           前端传来的 aid
     * @param uid           session中获取到的uid
     * @param username      session中获取到的uid
     * @param cids          前端多选 传来的 cids...
     * @return Order        返回一个可以在前端展示的Order订单
     */
    @Override
    public Order create(Integer aid, Integer uid, String username, Integer[] cids) {
        List<CartVO> cartVOs = cartService.findByCids(cids, uid);
        Address address = addressService.findByAid(aid);

        BigDecimal totalPrice = new BigDecimal(0);

        Order order = new Order();
        order.setUid(uid);

        // 收货地址信息
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());

        // 设置总价、状态
        order.setTotalPrice(totalPrice);
        order.setStatus(0);

        // 设置时间
        order.setOrderTime(new Date());
        order.setPayTime(null);

        // BaseField
        order.setCreatedTime(new Date());
        order.setModifiedTime(new Date());
        order.setCreatedUser(username);
        order.setModifiedUser(username);

        Integer rows = orderMapper.insert(order);
        if(rows < 1){
            throw new InsertException("order 数据插入异常");
        }

        for (CartVO cartVO : cartVOs) {

            // 设置订单-商品项目：
            OrderItem orderItem = new OrderItem();
            orderItem.setOid(order.getOid());
            orderItem.setPid(cartVO.getPid());
            orderItem.setImage(cartVO.getImage());
            orderItem.setTitle(cartVO.getTitle());
            orderItem.setPrice(cartVO.getPrice());
            orderItem.setNum(cartVO.getNum());

            // BaseField
            orderItem.setCreatedTime(new Date());
            orderItem.setModifiedTime(new Date());
            orderItem.setCreatedUser(username);
            orderItem.setModifiedUser(username);

            // 计算订单的总价格
            totalPrice = totalPrice.add(cartVO.getRealPrice().multiply(new BigDecimal(cartVO.getNum())));

            Integer rows2 = orderItemMapper.insert(orderItem);
            if(rows2 < 1){
                throw new InsertException("order-item 数据插入异常");
            }
        }

        return order;
    }
}
